package Dao;

import Model.StudentCourse;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


public class ScoreDao extends BaseDao<StudentCourse> {
    StudentCourse studentCourse = new StudentCourse();

    /**
     *添加或者更改课程
     * @param course 课程名
     * @param student 学生姓名
     * @param score 分数
     * @return 操作成功，返回真
     * @author 张潇川
     */
    public Boolean changeScore(String course, String student, int score) {
        String sql = "UPDATE student_course SET " + course + " = ? WHERE student = ?";
        if (!(score >= 0 && score <= 100)) {
            System.out.println(" 输入成绩超出范围，请重新输入！");
            return false;
        }
        studentCourse.setScore(score);
        studentCourse.setName(student);
        alter(sql, studentCourse);//更新当前课程成绩
        this.total(student);//在数据库中更新总成绩
        this.aver(student);//在数据库中更新平均成绩
        return true;
    }

    /**
     * 每更新一次数据，都要重新计算总分
     * @param student 学生姓名
     * @author 张潇川
     */
    private void total(String student){
        OnePara onePara = new OnePara();
        ArrayList<StudentCourse> searchAllScore = onePara.selectOne(student);
        int totalPoints = 0 ;
        for(int everyPoint:searchAllScore.get(0).getCourseList())
        {
            totalPoints += everyPoint;
        }
        String sql = "UPDATE student_course SET totalPoint = ? where student = ?";
        studentCourse.setName(student);
        studentCourse.setScore(totalPoints);
        alter(sql,studentCourse);
    }

    /**
     * 每更新一次数据，都要重新计算总分
     * @param student
     * @author 张潇川
     */
    private void aver(String student){
        OnePara onePara = new OnePara();
        ArrayList<StudentCourse> searchAllScore = onePara.selectOne(student);
        int averPoints = 0 ;
        for(int everyPoint:searchAllScore.get(0).getCourseList())
        {
            averPoints += everyPoint;
        }
        String sql = "UPDATE student_course SET averPoint = ? WHERE student = ?";
        studentCourse.setName(student);
        studentCourse.setScore(averPoints/searchAllScore.get(0).getCourseList().size());
        alter(sql,studentCourse);


    }

    @Override
    public void insert(PreparedStatement preparedStatement, StudentCourse Object) {
        try {
            preparedStatement.setInt(1, Object.getScore());
            preparedStatement.setString(2, Object.getName());

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public StudentCourse setObjectByResultSet(ResultSet resultSet, int count) {

        StudentCourse studentCourse = new StudentCourse();
        try {
            studentCourse.setName(resultSet.getString(2));
            while (count > 4) {//循环存入
                studentCourse.setCourseList(resultSet.getInt(count));
                count--;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return studentCourse;

    }
}
